var http = require('http')
var _url = require('url')
var iconv = require('iconv-lite')
var BufferHelper = require('bufferhelper')

function download(url, cod, callback) {
    http.get(url, function (res) {
        var bufferHelper = new BufferHelper()
        res.on('data', function (chunk) {
            bufferHelper.concat(chunk)
        })
        res.on("end", function () {
            var val = iconv.decode(bufferHelper.toBuffer(), cod) 
            callback(val);
         })
    }).on("error", function () {
        callback(null)
    })
}

module.exports = download

